package com.ming.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ming.dao.TbUserMapper;
import com.ming.entity.TbUser;
import com.ming.param.UPpassword;
import com.ming.service.TbUserService;
import com.ming.shiro.RewriteShiroSessionDao;
import com.ming.util.BeanValidator;
import com.ming.util.JsonData;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @author MING
 * @date 2018/8/10 11:15
 */
@Service
public class TbUserServiceImpl implements TbUserService {
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private RewriteShiroSessionDao rewriteShiroSessionDao;

    @Override
    public TbUser login(String username) {
        return tbUserMapper.getUsername(username);
    }

    @Override
    @Transactional
    public JsonData UPpassword(UPpassword uPpassword, HttpServletRequest request) {
        BeanValidator.check(uPpassword);
        String str = (String)request.getSession().getAttribute("TbUser");
        TbUser tbUser = JSONObject.parseObject(str, TbUser.class);
        if (!uPpassword.getNewPwd().equals(uPpassword.getConfirmPwd())){
            return JsonData.fail("两次密码确认不一致！");
        }
        Md5Hash hash = new Md5Hash(uPpassword.getOldPwd());
        if (!hash.toString().equals(tbUser.getUserPassword())){
            return JsonData.fail("旧密码输入不正确！");
        }
        Md5Hash newPwd = new Md5Hash(uPpassword.getNewPwd());
        tbUser.setUserPassword(newPwd.toString());
        int update = tbUserMapper.update(tbUser);

        if (update != 1){
            return JsonData.fail("修改失败！");
        }else {
            rewriteShiroSessionDao.delete(SecurityUtils.getSubject().getSession());
            return JsonData.success("修改成功！请重新登录后刷新页面");
        }

    }
//
//    @Override
//    public Pager getUserPage(Integer pager, Integer limit) {
//        PageHelper.startPage(pager, limit);
//        List<UserTable> articleTableList = userTableMapper.getAll();
//        Pager page = Pager.toPage(new PageInfo(articleTableList));
//        return page;
//    }
//
//    @Override
//    public UserTable getUserTable(Integer userId) {
//        return userTableMapper.getUserTable(userId);
//    }
//
//    @Override
//    public JsonData saveOrUpdate(UserTable userTable) {
//        BeanValidator.check(userTable);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        userTable.setUserDate(sdf.format(new Date()));
//        int i;
//        if (userTable.getUserId() != null && !(userTable.getUserId()).equals("")) {
//            try {
//                if (userTableMapper.getUserUsername(userTable.getUserUsername()) != null){
//                    if (userTableMapper.getUserUsername(userTable.getUserUsername()).getUserId() != userTable.getUserId()){
//                        return JsonData.fail("此名存在，修改失败！");
//                    }
//                }
//            }catch (Exception e){
//                throw new ParamException("此名存在，修改失败！");
//            }
//            i = userTableMapper.update(userTable);
//        } else {
//            try {
//                if (userTableMapper.getUserUsername(userTable.getUserUsername()) != null){
//                    return JsonData.fail("此名存在，不可重复添加！");
//                }
//            }catch (Exception e){
//                throw new ParamException("此名存在，不可重复添加！");
//            }
//            userTable.setUserPassword(EncryptUtil.encryptMD5("666666"));
//            i = userTableMapper.insert(userTable);
//        }
//        if (i != 0) {
//            return JsonData.success("执行成功！");
//        }
//        return JsonData.fail("执行失败！");
//    }
//
//    @Override
//    public JsonData deleteId(Integer id) {
//        String[] ids = {id.toString()};
//        int i = userTableMapper.batchDeleteId(ids);
//        if (i != 0) {
//            return JsonData.success("删除成功！");
//        }
//        return JsonData.fail("删除失败！");
//    }
}
